const mongoose = require('../util/db')
const userSchema =new mongoose.Schema({
   username:String,
   password:String,
   avatar:String
})
const User = mongoose.model('users',userSchema)
const isUserExist = (userinfo) => {
   return new Promise((resolve,reject)=>{
      User.find(userinfo, (err,docs)=>{
         if (err) throw err
         else {
            if(docs.length > 0){
               //用户存在，把用户信息返回
               resolve(docs[0])
            }else {
               reject()
            }
         }
      })
   })
 }

 const saveOneUser=(userinfo)=>{
   const user = new User(userinfo)
   //user.save不写回调的时候
   return user.save()
 }
 module.exports = {
    isUserExist,
    saveOneUser,
 }